package cn.zb.zbrhback.auth.controller;

import cn.zb.zbrhback.auth.annotation.ZBPermission;
import cn.zb.zbrhback.auth.domain.Permission;
import cn.zb.zbrhback.auth.domain.PermissionQuery;
import cn.zb.zbrhback.auth.service.IPermissionService;
import cn.zb.zbrhback.basic.util.AjaxResult;
import cn.zb.zbrhback.basic.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@ZBPermission(name = "权限管理",desc = "权限管理")
@Api(value = "权限管理Api",tags = {"权限操作接口"})
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页条件查询权限
     */
    @PostMapping
    @ApiOperation(value = "分页条件查询权限",notes = "参数为当前页，每页条数和查询条件，条件可空")
    @ZBPermission(name = "分页条件查询权限",desc = "分页条件查询权限")
    public AjaxResult pageAll(@RequestBody PermissionQuery permissionQuery){
        try {
            System.out.println("条件"+permissionQuery.toString());
            PageResult<Permission> permissions = permissionService.findPage(permissionQuery);
            return AjaxResult.me().setData(permissions);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("分页条件查询权限失败"+e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "查询角色树",notes = "不需要条件")
    @ZBPermission(name = "查询角色树",desc = "查询角色树")
    public AjaxResult getTree(){
        try {
            List<Permission> permissions = permissionService.getTree();
            return AjaxResult.me().setData(permissions);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("角色树查询失败"+e.getMessage()).setSuccess(false);
        }
    }

    @GetMapping("/sns")
    @ApiOperation(value = "获取角色的唯一标识",notes = "参数角色id")
    @ZBPermission(name = "获取角色的唯一标识",desc = "获取角色的唯一标识")
    public AjaxResult getPermissionSns(){
        try {
            List<String> sns = permissionService.getPermissionSns();
            return AjaxResult.me().setData(sns);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取角色唯一标识失败"+e.getMessage());
        }
    }
}
